<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>地图</title>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    <style type="text/css">
        html,
        body,
        #container {
            height: 100%;
        }

        .weather {
            width: 5rem;
            display: inline-block;
            padding-left: 0.5rem;
        }

        .sharp {
            height: 1rem;
            width: 1rem;
            background-color: white;
            transform: rotateZ(45deg);
            box-shadow: 2px 2px 3px rgba(114, 124, 245, .5);
            position: inherit;
            margin-left: 10.5rem;
            margin-top: -6px;
        }

        #panel {
            position: fixed;
            background-color: white;
            max-height: 90%;
            overflow-y: auto;
            top: 10px;
            right: 10px;
            width: 280px;
        }

        #panel .amap-call {
            background-color: #009cf9;
            border-top-left-radius: 4px;
            border-top-right-radius: 4px;
        }

        #panel .amap-lib-driving {
            border-bottom-left-radius: 4px;
            border-bottom-right-radius: 4px;
            overflow: hidden;
        }
    </style>
</head>

<body>
    起点：<input type="text">
    终点：<input type="text">
    <button>开始规划</button>

    <div id="container"></div>

    <div class="info">
        <h4>预报天气</h4>
        <hr>
        <p id='forecast'></p>
    </div>

    <div id="panel"></div>


    <script type="text/javascript">
        window._AMapSecurityConfig = {
            securityJsCode: "4512b49f896bb873647045195d5df475",
        };
    </script>
    <script type="text/javascript"
        src="https://webapi.amap.com/maps?v=1.4.15&key=2a141d514e1e885d2b3a9fcb3a60a075"></script>
    <script type="text/javascript"
        src="https://webapi.amap.com/maps?v=1.4.15&key=2a141d514e1e885d2b3a9fcb3a60a075&plugin=AMap.Driving"></script>

    <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>

    <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
    
    <script type="text/javascript">
        var map = new AMap.Map('container', {
            resizeEnable: true,
            center: [113.383385, 34.7866],
            zoom: 12
        });
        AMap.plugin('AMap.Weather', function () {
            var weather = new AMap.Weather();
            //查询实时天气信息, 查询的城市到行政级别的城市，如朝阳区、杭州市
            weather.getLive('荥阳市', function (err, data) {
                if (!err) {
                    var str = [];
                    str.push('<h4 >实时天气' + '</h4><hr>');
                    str.push('<p>城市/区：' + data.city + '</p>');
                    str.push('<p>天气：' + data.weather + '</p>');
                    str.push('<p>温度：' + data.temperature + '℃</p>');
                    str.push('<p>风向：' + data.windDirection + '</p>');
                    str.push('<p>风力：' + data.windPower + ' 级</p>');
                    str.push('<p>空气湿度：' + data.humidity + '</p>');
                    str.push('<p>发布时间：' + data.reportTime + '</p>');
                    var marker = new AMap.Marker({ map: map, position: map.getCenter() });
                    var infoWin = new AMap.InfoWindow({
                        content: '<div class="info" style="position:inherit;margin-bottom:0;">' + str.join('') + '</div><div class="sharp"></div>',
                        isCustom: true,
                        offset: new AMap.Pixel(0, -37)
                    });
                    infoWin.open(map, marker.getPosition());
                    marker.on('mouseover', function () {
                        infoWin.open(map, marker.getPosition());
                    });
                }
            });
            //未来4天天气预报
            weather.getForecast('荥阳市', function (err, data) {
                if (err) { return; }
                var str = [];
                for (var i = 0, dayWeather; i < data.forecasts.length; i++) {
                    dayWeather = data.forecasts[i];
                    str.push(dayWeather.date + ' <span class="weather">' + dayWeather.dayWeather + '</span> ' + dayWeather.nightTemp + '~' + dayWeather.dayTemp + '℃');
                }
                document.getElementById('forecast').innerHTML = str.join('<br>');
            });
        });



        // 驾车路线规划
        //构造路线导航类
        // 获取元素
        var start = document.querySelectorAll('input')[0]
        var end = document.querySelectorAll('input')[1]
        document.querySelector('button').addEventListener('click', () => {
            var driving = new AMap.Driving({
                map: map,
                panel: "panel"
            });
            // 根据起终点名称规划驾车导航路线
            driving.search([
                { keyword: start.value, city: '北京' },
                { keyword: end.value, city: '北京' }
            ], function (status, result) {
                // result 即是对应的驾车导航信息，相关数据结构文档请参考  https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingResult
                if (status === 'complete') {
                    log.success('绘制驾车路线完成')
                } else {
                    log.error('获取驾车数据失败：' + result)
                }
            });
        })

    </script>
</body>

</html>